<!--微信资源分类管理-->
<template>
  <div id="thirdWechatResourceCategory" class="app-container">
    <!--搜索条件-->
    <div class="filter-container">
      <el-form :model="listQuery" :inline="true">
        <el-form-item>
          <application-key-select v-model="listQuery.appId" :type="1" />
        </el-form-item>
        <el-form-item>
          <el-input v-model="listQuery.keyword" placeholder="请输入关键字" @keyup.enter.native="handleFilter" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" plain @click="handleFilter">搜索</el-button>
          <el-button icon="el-icon-refresh" plain @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-tabs v-model="activeName" class="common-tabs">
      <el-tab-pane name="text">
        <span slot="label"><i class="el-icon-notebook-2" /> 文本</span>
      </el-tab-pane>
      <el-tab-pane name="image">
        <span slot="label"><i class="el-icon-picture" /> 图片</span>
      </el-tab-pane>
      <el-tab-pane name="audio">
        <span slot="label"><i class="el-icon-microphone" /> 语音</span>
      </el-tab-pane>
      <el-tab-pane name="video">
        <span slot="label"><i class="el-icon-video-play" /> 视频</span>
      </el-tab-pane>
      <el-tab-pane name="imageText">
        <span slot="label"><i class="el-icon-document" />图文</span>
      </el-tab-pane>
    </el-tabs>
    <!--列表-->
    <div class="table-container flex-main">
      <div class="table-container-header">
        <table-toolbar @create="handleCreate" @refresh="handleFilter" @remove="removeRecord" />
      </div>
      <el-table
        ref="multipleTable"
        :data="list"
        header-cell-class-name="table-header"
        fit
        row-key="id"
        default-expand-all
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
        height="100%"
        highlight-current-row
        @selection-change="handleSelectionChange"
        @row-click="handleRowClick"
        @sort-change="sortChange">
        <el-table-column type="selection" width="60" />
        <el-table-column align="center" prop="id" label="#" width="60">
          <template slot-scope="scope">{{ scope.$index+1 }}</template>
        </el-table-column>
        <el-table-column label="分类名称" prop="name" min-width="200" sortable show-overflow-tooltip />
        <el-table-column prop="gmtModified" sortable label="修改时间" width="160">
          <template slot-scope="scope">{{ scope.row.gmtModified | formatDate }}</template>
        </el-table-column>
        <el-table-column label="操作" align="center" fixed="right" width="90">
          <template slot-scope="scope">
            <button-option icon="el-icon-edit" skin="primary" title="编辑" @click="handleUpdate(scope.row)" />
            <button-option icon="el-icon-delete" skin="danger" title="删除" @click="handleRemoveSingle(scope.row)" />
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!--新增/编辑侧边栏-->
    <side-box :show="sideBox.recordShow" :title="sideBox.title" @close="sideBox.recordShow = !sideBox.recordShow">
      <template slot="body">
        <el-form
          ref="dataForm"
          :rules="thirdWechatResourceCategoryRules"
          :model="thirdWechatResourceCategoryRecord"
          label-position="right"
          label-width="120px">
          <el-form-item label="AppId:" prop="appId">
            <el-input v-model="thirdWechatResourceCategoryRecord.appId" disabled maxlength="20" show-word-limit placeholder="请输入" />
          </el-form-item>
          <el-form-item label="分类名称:" prop="name">
            <el-input v-model="thirdWechatResourceCategoryRecord.name" maxlength="20" show-word-limit placeholder="请输入" />
          </el-form-item>
          <el-form-item v-if="listQuery.type===4" label="所属上级:" prop="pid">
            <el-select v-model="thirdWechatResourceCategoryRecord.pid" placeholder="选择所属上级" style="width:100%">
              <el-option label="根目录" value="0" />
              <el-option v-for="type in pidOptions" :key="type.value" :label="type.label" :value="type.value" />
            </el-select>
          </el-form-item>

        </el-form>
      </template>
      <template slot="foot">
        <el-button type="primary" plain @click="sideBox.status==='create'?createData():updateData()">保存 </el-button>
      </template>
    </side-box>
  </div>
</template>

<script>
import applicationKeySelect from '../applicationKeySelect'
export default {
  components: { applicationKeySelect },

  data() {
    return {
      activeName: 'text',
      list: [],
      listQuery: {
        appId: '',
        type: 3,
        page: 1,
        limit: this.common.PAGE_LIMIT_DEFAULT,
        keyword: undefined,
        sort: this.common.SORT_DEFAULT
      },

      multipleSelectionRecords: [],
      // 固定写法
      sideBox: {
        recordShow: false,
        title: '',
        status: 'create'
      },
      pidOptions: [],
      thirdWechatResourceCategoryRecord: {},
      thirdWechatResourceCategoryRules: {
        name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ],
        pid: [
          { required: true, message: '请输入父级id', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    'listQuery.appId': function(val) {
      this.fetchData()
    },
    activeName: function(val) {
      var type = 0
      if (val === 'image') {
        type = 0
      }
      if (val === 'audio') {
        type = 1
      }
      if (val === 'video') {
        type = 2
      }
      if (val === 'text') {
        type = 3
      }
      if (val === 'imageText') {
        type = 4
      }
      this.listQuery = {
        ...this.listQuery,
        type: type
      }
      this.fetchData()
    }
  },
  created() {
  },
  methods: {
    // 表格自带方法开始
    /* 全选/反选*/
    handleSelectionChange(val) {
      this.multipleSelectionRecords = val
    },
    /* 表格行数据单击*/
    handleRowClick(row) {
      this.$refs.multipleTable.toggleRowSelection(row)
    },
    /* 表格列排序*/
    sortChange(data) {
      const { prop, order } = data
      if (prop) {
        if (order === 'ascending') {
          this.listQuery.sort = '+' + prop
        } else {
          this.listQuery.sort = '-' + prop
        }
      } else {
        this.listQuery.sort = this.common.SORT_DEFAULT
      }
      this.handleFilter()
    },
    // 表格自带方法结束

    /* 搜索*/
    handleFilter() {
      this.listQuery.page = 1
      this.fetchData()
    },
    /* 重置*/
    handleReset() {
      this.listQuery = {
        ...this.listQuery,
        page: 1,
        limit: this.common.PAGE_LIMIT_DEFAULT,
        keyword: undefined,
        sort: this.common.SORT_DEFAULT
      }
      this.fetchData()
    },
    async fetchData() {
      const response = await this.$store.dispatch('thirdWechatResourceCategoryTree', this.listQuery)
      this.list = response
    },
    /* 新增*/
    handleCreate() {
      this.thirdWechatResourceCategoryRecord = {
        type: this.listQuery.type,
        appId: this.listQuery.appId,
        pid: '0'
      }
      this.sideBox.status = 'create'
      this.sideBox.recordShow = true
      this.sideBox.title = '新增微信资源分类'
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          var tempData = Object.assign({}, this.thirdWechatResourceCategoryRecord)
          delete tempData.children
          this.$store.dispatch('thirdWechatResourceCategorySave', tempData).then(response => {
            this.sideBox.recordShow = false
            this.$modal.msgSuccess('创建成功')
            this.handleReset()
          }).catch(() => {
            this.sideBox.recordShow = true
          })
        }
      })
    },
    /* 编辑*/
    handleUpdate(row) {
      this.thirdWechatResourceCategoryRecord = Object.assign({}, row) // copy obj
      this.sideBox.status = 'update'
      this.sideBox.recordShow = true
      this.sideBox.title = '编辑微信资源分类'
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          const tempData = Object.assign({}, this.thirdWechatResourceCategoryRecord)
          delete tempData.children
          this.$store.dispatch('thirdWechatResourceCategoryUpdate', tempData).then(response => {
            this.sideBox.recordShow = false
            this.$modal.msgSuccess('修改成功')
            this.handleFilter()
          }).catch(() => {
            this.sideBox.recordShow = true
          })
        }
      })
    },

    /* 删除*/
    handleRemoveSingle(row) {
      this.multipleSelectionRecords = []
      this.multipleSelectionRecords.push(row)
      this.$refs.multipleTable.clearSelection()
      this.$refs.multipleTable.toggleRowSelection(row, true)
      this.removeRecord()
    },
    removeRecord() {
      if (this.multipleSelectionRecords.length > 0) {
        const ids = []
        this.multipleSelectionRecords.forEach(function(v, i) {
          ids.push(v.id)
        })
        this.$confirm('此操作将永久删除记录, 是否继续?', '删除提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$store.dispatch('thirdWechatResourceCategoryRemove', ids).then(response => {
            this.handleReset()
            this.$modal.msgSuccess('删除成功')
          }).catch(() => {})
        }).catch(() => { })
      } else {
        this.$modal.msgWarning('请选择要删除的记录')
      }
    }

  }
}
</script>
<style lang="scss" scoped>
</style>
